"""
Author: wenbin
Create: 2024-08-15
"""
import uuid
from dataclasses import dataclass

from flask import Response
from injector import inject

from app.libs import Router
from app.schema.response import ApiResponse
from app.service import AppService
from app.validator import ValidCreateApp, ValidDebug

router = Router("/app")


@inject
@dataclass
class AppView:
    """
    应用
    """
    app_service: AppService

    @router.bind(methods=["POST"])
    def create_app(self) -> Response:
        """创建应用"""
        valid = ValidCreateApp().validate_parameter()
        self.app_service.create(valid)
        return ApiResponse.success(message="创建成功")

    @router.bind("<uuid:app_id>")
    def get_app(self, app_id: uuid.UUID) -> Response:
        """查询应用"""
        app = self.app_service.get(app_id)
        return ApiResponse.success(data=app)

    @router.bind(methods=["PUT"])
    def update_app(self) -> Response:
        """修改应用"""
        return ApiResponse.success(message="修改成功")

    @router.bind(methods=["DELETE"])
    def delete_app(self) -> Response:
        """删除应用"""
        return ApiResponse.success(message="删除成功")

    @router.bind("<uuid:app_id>/debug", methods=["POST"])
    def debug(self, app_id: uuid.UUID) -> Response:
        """应用对话调试"""
        valid = ValidDebug().validate_parameter()
        content = self.app_service.debug(app_id, valid.question.data)
        return ApiResponse.success(data={
            "content": content
        })
